/**********************************************************************
 *@Project： CloudTest
 *@File: DictController
 *@Date: 2017/3/3
 *@Copyright (C) 2013-2017 http://www.posun.cn. All Rights Reserved.
 ***********************************************************************
 *注意： 本内容仅限于深圳市普盛实业有限公司内部使用，禁止外泄
 **********************************************************************
 */
package com.posun.edu.system.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.posun.cloud.tracing.TraceUtil;
import com.posun.edu.system.entity.Dict;
import com.posun.edu.system.service.IDictService;
import com.posun.framework.dto.DataSourceResult;
import com.posun.framework.orm.domain.Searchable;
import com.posun.framework.orm.enums.Operator;
import com.posun.framework.util.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * TODO<br>
 *
 * @author YL
 * @version 1.0
 * @createTime 2017/3/3 17:27
 * @ChangeLog
 */
@Api(value = "/dict", description = "数据字典API清单", consumes = "application/json")
@RestController
@RequestMapping("/dict")
public class DictController {

    private static final Logger log = LoggerFactory.getLogger(DictController.class);

    @Resource
    private IDictService dictService;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView index(Model model) {
        return new ModelAndView("/system/dict/index");
    }

    @ApiOperation(value = "查看字段列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public DataSourceResult list(HttpServletRequest request) {

        Dict dict = new Dict();
        dict.setId("test");
        dict.setDictName("Test");
        //在SSO中放入用户鉴权信息
        Map<String, String> extMap = Maps.newHashMap();
        extMap.put("empId", "admin");
        extMap.put("user", JSON.toJSONString(dict));
        extMap.put(TraceUtil.TENANT_KEY, "test");
        TraceUtil.putAttachment(extMap);

        System.out.println(request.getSession().getId());
        System.out.println(request.getSession().getAttribute("randomId").toString());
        Searchable<Dict> searchable = new Searchable(new Dict());
        searchable.addSearch("id", Operator.PRE_LIKE, "8");
        searchable.setPage(1, 5);
        List<Dict> dicts = dictService.selectList(searchable);
        dicts.forEach(dict1 -> System.out.println(JSON.toJSONString(dict1)));

        searchable = new Searchable(new Dict());
        searchable.addSearch("id", Operator.PRE_LIKE, "85");
        searchable.setPage(1, 5);
        searchable.addAggregate("dictName", "count");
        DataSourceResult result = dictService.selectResult(searchable);
        System.out.println(JSON.toJSONString(result));

        Dict updateDict = dictService.selectById("853158441487749120");
        updateDict.setDeleteTime(LocalDateTime.now());
        updateDict.setDictName("test:" + DateUtil.getCurrentDateTime());

        boolean updateSuccess = dictService.update(updateDict);
        return result;
    }


    @ApiOperation(value = "查看某个字典的信息")

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Dict view(@ApiParam(name = "id", required = true, value = "字典编号") @PathVariable("id") String id) {
        return dictService.selectById(id);
    }

    @RequestMapping(value = "error", method = RequestMethod.GET)
    public String testException() {
        throw new RuntimeException("出错了，请联系系统管理员！");
    }
}
